﻿/*****************************************************************
// Copyright (C)
// All rights reserved.
// 
// Author:       WYQ
// Create Date:  2012-8-18 15:15:58
// Usage: 持久化数据库仓库
*****************************************************************/
using System;
using System.Linq;
using System.Linq.Expressions;

namespace Portal.Ef.Repository
{

   
    public class  AdvertisementRepository : Repository<Advertisement, Advertisement, TbAdvertisement>, IAdvertisementRepository<Advertisement>
    {
        public override Advertisement CreateEntity()
        {
            return new Advertisement { };
        }
        public override IQueryable<Advertisement> Get()
        {
            return MapQueryable(Entities.TbAdvertisement);
        }
        public override long Count()
        {
            return MapQueryable(Entities.TbAdvertisement).Count();
        }
        public override IQueryable<Advertisement> Get(Expression<Func<Advertisement, bool>> expression)
        {
            return MapQueryable(Entities.TbAdvertisement).Where(expression);
        }
        public override IQueryable<Advertisement> GetSort(string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbAdvertisement), sort, order);
        }
        public override IQueryable<Advertisement> GetSort(Expression<Func<Advertisement, bool>> expression, string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(expression, "查询条件");
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbAdvertisement).Where(expression), sort, order);
        }
        public override void Add(Advertisement t)
        {
            Entities.AddToTbAdvertisement(ReverseMapModel(t));
        }
        public override void Update(Advertisement t)
        {
            throw new NotImplementedException();
        }
        public override void AddOrUpdate(Advertisement t)
        {
            if (Entities.TbAdvertisement.Any(o => o.CoAdvertisementId == t.Id))
            {

            }
            else
            {
                Entities.AddToTbAdvertisement(ReverseMapModel(t));
            }
        }
        public override void Delete(Advertisement t)
        {
            Entities.TbAdvertisement.Where(o => o.CoAdvertisementId == t.Id).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Delete(Expression<Func<Advertisement, bool>> expression)
        {
            MapQueryable(Entities.TbAdvertisement).Where(expression).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Save()
        {
            Entities.SaveChanges();
        }
       
        public JqEasyuiDataGrid<Advertisement> JqEasyuiDataGrid(string code, string name, string categoryId, string sort = "CreateTime", string order = "Desc", int page = 10, int index = 1)
        {
            return new JqEasyuiDataGrid<Advertisement>();
        }
      
        public override sealed IQueryable<Advertisement> MapQueryable(IQueryable<TbAdvertisement> queryable)
        {
            return queryable.Select(o => new Advertisement 
            {
         
	           Id=o.CoAdvertisementId,
	           Name = o.CoName 
,
	           CreateTime = o.CoCreateTime??DateTime.Now 
,
	           IsDelete = o.CoIsDelete??false 
,
	           Version = o.CoVersion??0 
,
	           IsClosed = o.CoIsClosed??false 
,
	           Code = o.CoCode 
,
	           CategoryId = o.CoCategoryId 
,
	           Url = o.CoUrl 
,
	           Description = o.CoDescription 
,
	           Sort = o.CoSort??0 
,
	           Culture = o.CoCulture 
            
            });
        }
      
        public override sealed IQueryable<TbAdvertisement> ReverseMapQueryable(IQueryable<Advertisement> queryable)
        {
            return queryable.Select(o => new TbAdvertisement 
            { 
        
	           CoAdvertisementId=o.Id,
	           CoName = o.Name
,
	           CoCreateTime = o.CreateTime
,
	           CoIsDelete = o.IsDelete
,
	           CoVersion = o.Version
,
	           CoIsClosed = o.IsClosed
,
	           CoCode = o.Code
,
	           CoCategoryId = o.CategoryId
,
	           CoUrl = o.Url
,
	           CoDescription = o.Description
,
	           CoSort = o.Sort
,
	           CoCulture = o.Culture
  
            
            });
        }
      
        public override sealed Advertisement MapModel(TbAdvertisement model)
        {
            return new Advertisement {
	           Id=model.CoAdvertisementId,
	           Name = model.CoName
,
	           CreateTime = model.CoCreateTime??DateTime.Now
,
	           IsDelete = model.CoIsDelete??false
,
	           Version = model.CoVersion??0
,
	           IsClosed = model.CoIsClosed??false
,
	           Code = model.CoCode
,
	           CategoryId = model.CoCategoryId
,
	           Url = model.CoUrl
,
	           Description = model.CoDescription
,
	           Sort = model.CoSort??0
,
	           Culture = model.CoCulture
            };
        }
        
        public override sealed TbAdvertisement ReverseMapModel(Advertisement model)
        {
            return new TbAdvertisement { 
	           CoAdvertisementId=model.Id,
	           CoName = model.Name
	        
,
	           CoCreateTime = model.CreateTime
	        
,
	           CoIsDelete = model.IsDelete
	        
,
	           CoVersion = model.Version
	        
,
	           CoIsClosed = model.IsClosed
	        
,
	           CoCode = model.Code
	        
,
	           CoCategoryId = model.CategoryId
	        
,
	           CoUrl = model.Url
	        
,
	           CoDescription = model.Description
	        
,
	           CoSort = model.Sort
	        
,
	           CoCulture = model.Culture
	        
            
            
            };
        }


    }
}



